# Contributor: Josip <bpisoj@gmail.com>

_realname=PyICU
pkgbase=mingw-w64-python-icu
pkgname=("${MINGW_PACKAGE_PREFIX}-python-icu")
provides=("${MINGW_PACKAGE_PREFIX}-python3-icu")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-icu")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-icu")
pkgver=2.11
pkgrel=3
pkgdesc="Python extension wrapping the ICU C++ API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('BSD')
url="https://gitlab.pyicu.org/main/pyicu"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-icu")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("https://pypi.io/packages/source/P/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-icu-config.patch
        002-fix-mingw-build.patch)
sha256sums=('3ab531264cfe9132b3d2ac5d708da9a4649d25f6e6813730ac88cf040a08a844'
            '3e0e8155c2301c53b6e78a86a5de14ee3a958533c819bac435719eeb554216a0'
            'cf9b37b4862116a2717639163adb4dc262beb611a0c588e05912ec5943cd1609')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/001-icu-config.patch
  patch -p1 -i ${srcdir}/002-fix-mingw-build.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  export PYICU_LFLAGS="-L${MINGW_PREFIX}/lib"
  export PYICU_INCLUDES="${MINGW_PREFIX}/include"
  export PYICU_LIBRARIES="icuin;icuuc;icudt"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-icu/COPYING"
}
